import sqlite3

def check_database_structure():
    """检查investment_history表的结构"""
    try:
        # 连接数据库
        with sqlite3.connect('investment_history.db') as conn:
            cursor = conn.cursor()
            
            # 检查表结构
            print("investment_history表结构:")
            cursor.execute("PRAGMA table_info(investment_history)")
            columns = cursor.fetchall()
            for column in columns:
                print(f"  - {column[1]} ({column[2]}, NOT NULL: {column[3]})")
            
            # 查询前5条记录，验证user_id是否存在
            print("\n前5条记录（包含user_id）:")
            cursor.execute("SELECT id, date, cny, usd, exchange_rate, user_id FROM investment_history LIMIT 5")
            records = cursor.fetchall()
            for record in records:
                print(f"  - ID: {record[0]}, 日期: {record[1]}, 人民币: {record[2]}, 美元: {record[3]}, 汇率: {record[4]}, 用户ID: {record[5]}")
                
    except sqlite3.Error as e:
        print(f"检查数据库结构时发生错误: {e}")
    except Exception as e:
        print(f"发生未知错误: {e}")

if __name__ == '__main__':
    check_database_structure()